/********************************Copyright (c)**********************************\
**
**                   (c) Copyright 2020, Main, China, QD.
**                           All Rights Reserved
**
**                                 By(wo4fisher)
**                           http://www.wo4fisher.com
**
**----------------------------------文件信息------------------------------------
** 文件名称: bsp_ws2812.c
** 创建人员: wht
** 创建日期: 2020-01-20
** 文档描述:
**
**----------------------------------版本信息------------------------------------
** 版本代号: V0.1
** 版本说明: 初始版本
**
**------------------------------------------------------------------------------
\********************************End of Head************************************/
#include "bsp_ws2812.h"



volatile uint8_t  ws2812_xfer_flag = 0;
uint8_t pixelBuffer[PIXEL_NUM][GRB] = {0};


/*******************************************************************************
** 函数名称: WS281x_CloseAll
** 功能描述:
** 参数说明: None
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_CloseAll(void)
{
    uint8_t i;
    uint8_t j;

    for(i = 0; i < PIXEL_NUM; ++i)
    {
        for(j = 0; j < 24; ++j)
        {
            pixelBuffer[i][j] = WS_LOW;
        }
    }
    WS281x_Show();
}

/*******************************************************************************
** 函数名称: WS281x_Color
** 功能描述:
** 参数说明: red: [输入/出]
**			 green: [输入/出]
**			 blue: [输入/出]
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
uint32_t WS281x_Color(uint8_t red, uint8_t green, uint8_t blue)
{
    return green << 16 | red << 8 | blue;
}

/*******************************************************************************
** 函数名称: WS281x_SetPixelColor
** 功能描述: 
** 参数说明: n: [输入/出] 
**			 GRBColor: [输入/出] 
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_SetPixelColor(uint16_t n, uint32_t GRBColor)
{
    uint8_t i;
    if(n < PIXEL_NUM)
    {
        for(i = 0; i < GRB; i++)
        {
            pixelBuffer[n][i] = ((GRBColor << i) & 0x800000) ? WS_HIGH : WS_LOW;
        }
    }
}

/*******************************************************************************
** 函数名称: WS281x_SetPixelRGB
** 功能描述:
** 参数说明: n: [输入/出]
**			 red: [输入/出]
**			 green: [输入/出]
**			 blue: [输入/出]
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_SetPixelRGB(uint16_t n , uint8_t red, uint8_t green, uint8_t blue)
{
    uint8_t i;

    if(n < PIXEL_NUM)
    {
        for(i = 0; i < GRB; ++i)
        {
            pixelBuffer[n][i] = (((WS281x_Color(red, green, blue) << i) & 0X800000) ? WS_HIGH : WS_LOW);
        }
    }
}


/*******************************************************************************
** 函数名称: WS281x_Show
** 功能描述:
** 参数说明: None
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_Show(void)
{
    LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_7, LEN);
    LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_7);
    LL_TIM_EnableCounter(TIM1);
    while(ws2812_xfer_flag);
}

/*******************************************************************************
** 函数名称: WS281x_Wheel
** 功能描述: Input a value 0 to 255 to get a color value. The colours are a transition r - g - b - back to r.
** 参数说明: wheelPos: [输入/出]
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
uint32_t WS281x_Wheel(uint8_t wheelPos)
{
    wheelPos = 255 - wheelPos;
    if(wheelPos < 85)
    {
        return WS281x_Color(255 - wheelPos * 3, 0, wheelPos * 3);
    }
    if(wheelPos < 170)
    {
        wheelPos -= 85;
        return WS281x_Color(0, wheelPos * 3, 255 - wheelPos * 3);
    }
    wheelPos -= 170;
    return WS281x_Color(wheelPos * 3, 255 - wheelPos * 3, 0);
}


/*******************************************************************************
** 函数名称: WS281x_ColorWipe
** 功能描述: Fill the dots one after the other with a color
** 参数说明: c: [输入/出]
**			 wait: [输入/出]
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_ColorWipe(uint32_t c, uint8_t wait)
{
    for(uint16_t i = 0; i < PIXEL_NUM; i++)
    {
        WS281x_SetPixelColor(i, c);
        WS281x_Show();
        LL_mDelay(wait);
    }
}

/*******************************************************************************
** 函数名称: WS281x_Rainbow
** 功能描述:
** 参数说明: wait: [输入/出]
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_Rainbow(uint8_t wait)
{
    uint16_t i, j;

    for(j = 0; j < 256; j++)
    {
        for(i = 0; i < PIXEL_NUM; i++)
        {
            WS281x_SetPixelColor(i, WS281x_Wheel((i + j) & 255));
        }
        WS281x_Show();
        LL_mDelay(wait);
    }
}


/*******************************************************************************
** 函数名称: WS281x_RainbowCycle
** 功能描述: Slightly different, this makes the rainbow equally distributed throughout
** 参数说明: wait: [输入/出] 
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_RainbowCycle(uint8_t wait)
{
    uint16_t i, j;

    for(j = 0; j < 256 * 5; j++) // 5 cycles of all colors on wheel
    {
        for(i = 0; i < PIXEL_NUM; i++)
        {
            WS281x_SetPixelColor(i, WS281x_Wheel(((i * 256 / PIXEL_NUM) + j) & 255));
        }
        WS281x_Show();
        LL_mDelay(wait);
    }
}


/*******************************************************************************
** 函数名称: WS281x_TheaterChase
** 功能描述: Theatre-style crawling lights.
** 参数说明: c: [输入/出] 
**			 wait: [输入/出] 
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_TheaterChase(uint32_t c, uint8_t wait)
{
    for (int j = 0; j < 10; j++) //do 10 cycles of chasing
    {
        for (int q = 0; q < 3; q++)
        {
            for (uint16_t i = 0; i < PIXEL_NUM; i = i + 3)
            {
                WS281x_SetPixelColor(i + q, c);  //turn every third pixel on
            }
            WS281x_Show();

            LL_mDelay(wait);

            for (uint16_t i = 0; i < PIXEL_NUM; i = i + 3)
            {
                WS281x_SetPixelColor(i + q, 0);      //turn every third pixel off
            }
        }
    }
}

/*******************************************************************************
** 函数名称: WS281x_TheaterChaseRainbow
** 功能描述: Theatre-style crawling lights with rainbow effect
** 参数说明: wait: [输入/出] 
** 返回说明: None
** 创建人员: wht
** 创建日期: 2020-01-21
**------------------------------------------------------------------------------
** 修改人员:wht
** 修改日期:
** 修改描述:
**------------------------------------------------------------------------------
********************************************************************************/
void WS281x_TheaterChaseRainbow(uint8_t wait)
{
    for (int j = 0; j < 256; j++)     // cycle all 256 colors in the wheel
    {
        for (int q = 0; q < 3; q++)
        {
            for (uint16_t i = 0; i < PIXEL_NUM; i = i + 3)
            {
                WS281x_SetPixelColor(i + q, WS281x_Wheel( (i + j) % 255)); //turn every third pixel on
            }
            WS281x_Show();

            LL_mDelay(wait);

            for (uint16_t i = 0; i < PIXEL_NUM; i = i + 3)
            {
                WS281x_SetPixelColor(i + q, 0);      //turn every third pixel off
            }
        }
    }
}

/********************************End of File************************************/
